package net.csdn.business.kanban.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import net.csdn.business.kanban.api.entity.KanbanView;
import net.csdn.business.kanban.api.mapper.KanbanViewMapper;
import net.csdn.business.kanban.api.service.KanbanViewService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName KanbanServiceImpl
 * @Description TODO
 * @Author liuzhisheng
 * @Date 2023/9/13
 **/
@Service
@RequiredArgsConstructor
public class KanbanViewServiceImpl extends ServiceImpl<KanbanViewMapper, KanbanView> implements KanbanViewService {
    private final KanbanViewMapper kanbanViewMapper;

    @Override
    public boolean removeKanbanView(String kanbanId){
        int count = kanbanViewMapper.removeKanbanView(kanbanId);
        return count > 0 ? true : false;
    }

    @Override
    public HashMap<String, Integer> getViewMap(String kanbanId) {
        HashMap<String, Integer> viewMap = kanbanViewMapper.getViewMap(kanbanId);
        return viewMap==null? Maps.newHashMap():viewMap;
    }

    @Override
    public List<KanbanView> getAllKanbanViews(String kanbanId) {
        LambdaQueryWrapper<KanbanView> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KanbanView::getKanbanId,kanbanId);
        wrapper.orderByAsc(KanbanView::getPriority);
        return kanbanViewMapper.selectList(wrapper);
    }

    @Override
    public Boolean updateViewPriority(Long id, Integer priority,String kanbanId) {
        LambdaUpdateWrapper<KanbanView> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(KanbanView::getId,id).eq(KanbanView::getKanbanId,kanbanId)
                .set(KanbanView::getPriority,priority);
        return kanbanViewMapper.update(null,updateWrapper) > 0 ? true : false;
    }

    @Override
    public Boolean viewPriorityAfter(Integer priority,String kanbanId) {
        LambdaUpdateWrapper<KanbanView> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.setSql("priority=priority+1");
        updateWrapper.eq(KanbanView::getKanbanId,kanbanId);
        updateWrapper.ge(KanbanView::getPriority,priority);
        return kanbanViewMapper.update(null,updateWrapper) > 0 ? true : false;
    }

    @Override
    public KanbanView getKanbanView(String kanbanId, Long viewId) {
        LambdaQueryWrapper<KanbanView> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KanbanView::getKanbanId,kanbanId);
        wrapper.eq(KanbanView::getId,viewId);
        return kanbanViewMapper.selectOne(wrapper);
    }

    @Override
    public int updateViewFieldDisplayJson(Long fieldId,Boolean isDisplay, String kanbanId) {
        return kanbanViewMapper.updateViewFieldDisplayJson(fieldId, isDisplay, kanbanId);
    }

    @Override
    public Integer maxPriority(String kanbanId) {
        return kanbanViewMapper.maxPriority(kanbanId);
    }
}
